Cast dates

start_date <- "2017-01-01"
end_date <- "2019-12-31"
f1<-function(d2, d1){
  n_weeks <-  floor(as.numeric(difftime(d2, d1, units="weeks")))
}
f2<-function(d2, d1){
  n_weeks <- floor(as.numeric(difftime(as.Date(d2)
    , as.Date(d1), units = "weeks")))
}
m1<-microbenchmark(
  Nocast = f1(end_date, start_date),
  Cast = f2(end_date, start_date),
  times = 1000
)

print(m1)
## Unit: microseconds
##    expr     min      lq     mean   median       uq      max neval
##  Nocast 330.035 338.802 366.1580 343.2810 365.1665 3050.561  1000
##    Cast 118.381 121.978 134.5156 123.7055 131.9560 2713.642  1000
fig <- fbox_plot(m1, "microseconds")
fig

Explicit vector length vector(“type”, length) is faster than an empty vector c()

create_c <- function (n){
  x <- c()
  for (i in seq(n)) {
    x <- c(x, i)
  }
}
create_vector <- function (n){
  x <- vector("integer", n)
  for (i in seq(n)) {
    x[i] <- i
  }
}
m3 <- microbenchmark(
  with_c = create_c(1e4),
  with_vector = create_vector(1e4),
  times = 10
)
print(m3)
## Unit: microseconds
##         expr       min        lq       mean   median        uq       max neval
##       with_c 66038.125 66637.164 68856.0730 66846.37 67561.769 80055.678    10
##  with_vector   356.465   370.431   655.7262   393.97   401.259  3091.988    10
fig <- fbox_plot(m3, "microseconds")
fig

which function is slow for some simple situations

vector <- runif(1e8)
w1 <- function(x){
  d <- length(which(x > .5))
}
w2 <- function(x){
  d <- sum(x > .5)
}

m4 <- microbenchmark(
  which = w1(vector),
  nowhich = w2(vector),
  times = 10
)
print(m4)
## Unit: milliseconds
##     expr      min       lq     mean   median       uq      max neval
##    which 628.2919 631.6294 660.3250 632.9834 702.8313 768.0381    10
##  nowhich 219.1176 219.5851 221.5409 220.9255 224.0479 224.5021    10
fig <- fbox_plot(m4, "miliseconds")
fig

Column operation is faster than row operation

n <- 1e4
dt <- data.table(
  a = seq(n), b = runif(n)
)
v1 <- function(dt){
  d <- mean(dt[dt$b > .5, ]$a)
}
v2 <- function(dt){
  d <- mean(dt$a[dt$b > .5])
}
m5 <- microbenchmark(
  row_operation = v1(dt),
  column_operation = v2(dt),
  times = 10
)
print(m5)
## Unit: microseconds
##              expr     min      lq     mean  median      uq      max neval
##     row_operation 164.467 178.844 884.7827 187.765 211.345 5125.312    10
##  column_operation  63.017  68.137 283.8324  70.687  78.987 2107.412    10
fig <- fbox_plot(m5, "microseconds")
fig

Sequences function safer than 1:n

The function seq prevents when the second part of the 1:x is zero

num <- 1e7
s1 <- function(num){
  d <- mean(1:num)
}
s2 <- function(num){
  d <- mean(seq(num))
}
m6<-microbenchmark(
  noseq = s1(num),
  seq = s2(num),
  times = 30
)
print(m6)
## Unit: milliseconds
##   expr      min       lq     mean   median       uq      max neval
##  noseq 69.85479 69.96090 70.07312 70.01606 70.06430 71.68678    30
##    seq 69.87678 69.97798 70.07228 70.02682 70.06016 71.48165    30
fig <- fbox_plot(m6, "miliseconds")
fig

paste0 is faster than glue

large_dataset <- data.table(
  id = 1:1000000,
  value = sample(letters, 1000000, replace = TRUE)
)
a1 <- function(x){
  d <- x %>% mutate(code = paste0(id, "_", value))
}
a2 <- function(x){
  d <- x %>% mutate(code = glue("{id}_{value}"))
}
m7 <- microbenchmark(
  with_paste = a1(large_dataset),
  with_glue = a2(large_dataset),
  times = 20
)
print(m7)
## Unit: milliseconds
##        expr      min       lq     mean   median       uq      max neval
##  with_paste 563.6145 575.2132 580.8992 578.0203 585.4943 607.3986    20
##   with_glue 583.1836 589.3698 614.0820 594.8731 601.1121 965.8527    20
fig <- fbox_plot(m7, "miliseconds")
fig

for loop vs lapply

# Example data
data <- data.table(group = rep(seq(10), each = 100), value = rnorm(1000))
print(table(data$group))
## 
##   1   2   3   4   5   6   7   8   9  10 
## 100 100 100 100 100 100 100 100 100 100
# Using a for loop
for_loop_function <- function(data) {
  res <- list()
  unique_groups <- unique(data$group)
  for(this_group in unique_groups) {
    res[[this_group]] <- data %>% filter(group == this_group)
  }
  return(res)
}
sapply_function <- function(data){
  unique_groups <- unique(data$group)
  res <- list()
  sapply(unique_groups, function(this_group){
    res[[this_group]] <<- data %>% filter(group == this_group)
  })
  return(res)
}

m8 <- microbenchmark(
  for_loop = for_loop_function(data),
  sapply = sapply_function(data),
  times = 500
)

print(m8)
## Unit: milliseconds
##      expr      min       lq     mean   median       uq      max neval
##  for_loop 6.610062 6.758855 7.092695 6.809214 6.876074 17.75556   500
##    sapply 6.714737 6.836470 7.136992 6.882496 6.945609 27.07984   500
fig <- fbox_plot(m8, "miliseconds")
fig

Date vs IDate

## Unit: microseconds
##   expr      min       lq      mean    median        uq      max neval
##   Date 1464.382 1520.521 1688.4063 1542.8235 1728.7700 3965.679   200
##  iDate  572.799  600.345  681.6371  626.2235  649.5825 2402.492   200
fig <- fbox_plot(m9, "miliseconds")
fig

Base R switch vs Dplyr case_when (for simple tasks)

switch_function <- function(x) {
  switch(x,
         "a" = "apple",
         "b" = "banana",
         "c" = "cherry",
         "default")
}
case_when_function <- function(x) {
  case_when(
    x == "a" ~ "apple",
    x == "b" ~ "banana",
    x == "c" ~ "cherry",
    TRUE ~ "default"
  )
}
# Create a vector of test values
test_values <- sample(c("a", "b", "c", "d"), 1000, replace = TRUE)
m10 <- microbenchmark(
  switch = sapply(test_values, switch_function),
  case_when = sapply(test_values, case_when_function),
  times = 200L
)
print(m10)
## Unit: microseconds
##       expr        min         lq        mean      median          uq        max
##     switch    633.452    641.156    747.4343    647.8485    663.5075   9520.652
##  case_when 223495.741 232348.918 235780.3888 235294.2830 236867.4930 327207.864
##  neval
##    200
##    200
fig <- fbox_plot(m10, "microseconds")
fig

data.table fcase vs Dplyr case_when

set.seed(123)
n <- 1e6
data <- data.table(
  id = seq(n),
  value = sample(seq(100), n, replace = TRUE)
)

casewhenf <- function(data){
  df <- data %>%
    mutate(category = case_when(
      value <= 20 ~ "Low",
      value <= 70 ~ "Medium",
      value > 70 ~ "High"))
}
fcasef <- function(data){
  df <- data %>%
    mutate(category = fcase(
      value <= 20, "Low",
      value <= 70, "Medium",
      value > 70, "High"))
}
m11 <- microbenchmark(
  case_when = casewhenf(data),
  fcase = fcasef(data),
  times = 20
)
print(m11)
## Unit: milliseconds
##       expr      min       lq     mean   median       uq      max neval
##  case_when 56.48162 56.75932 60.64567 56.91790 64.56608 75.87161    20
##      fcase 20.58373 20.73047 21.54201 20.78755 20.91723 26.75514    20
fig <- fbox_plot(m11, "miliseconds")
fig

data.table fcoalesce vs tidyr replace_na

set.seed(123)
DT <- data.table(
  ID = 1:1e6,
  Value1 = sample(c(NA, 1:100), 1e6, replace = TRUE),
  Value2 = sample(c(NA, 101:200), 1e6, replace = TRUE)
)

# Define the functions
replace_na_f <- function(data){
  DF <- data %>%
    mutate(Value1 = replace_na(Value1, 0),
           Value2 = replace_na(Value2, 0)) %>%
    as.data.table()
}
fcoalesce_f <- function(data){
  DF <- data %>%
    mutate(Value1 = fcoalesce(Value1, 0L),
           Value2 = fcoalesce(Value2, 0L))
}
m12 <- microbenchmark(
  treplace_na = replace_na_f(DT),
  tfcoalesce = fcoalesce_f(DT),
  times = 20
)
print(m12)
## Unit: milliseconds
##         expr      min       lq      mean   median       uq      max neval
##  treplace_na 7.400617 7.745105 14.606019 7.893111 9.481610 80.23419    20
##   tfcoalesce 1.517451 1.703418  6.492432 1.897015 2.700749 62.61303    20
fig <- fbox_plot(m12, "miliseconds")
fig

data.table notation vs dplyr notation

dt <- data.table(field_name = c("argentina.blue.man.watch", 
                                "brazil.red.woman.shoes", 
                                "canada.green.kid.hat", 
                                "denmark.red.man.shirt"))

# Filter rows where 'field_name' does not contain 'red'
dtnot <- function(data){
  filtered_dt <- data |> _[!grepl("red", field_name)]
}
dplyrnot <- function(data){
  filtered_dt <- data %>% filter(!grepl("red", field_name))
}

m13 <- microbenchmark(
  tdtnot = dtnot(dt),
  tdplyrnot = dplyrnot(dt),
  times = 100
)
print(m13)
## Unit: microseconds
##       expr     min      lq     mean   median       uq     max neval
##     tdtnot 100.478 109.474 142.8368 129.1915 138.2230 1806.06   100
##  tdplyrnot 666.544 686.541 726.1847 698.1730 716.7525 2728.61   100
fig <- fbox_plot(m13, "microseconds")
fig

data.table melt vs tidyr pivot_longer

large_data <- data.table(
  id = 1:100000,
  var1 = rnorm(100000),
  var2 = rnorm(100000),
  var3 = rnorm(100000),
  var4 = rnorm(100000)
)
# Benchmarking
m14 <- microbenchmark(
  tidyr_pivot_longer = {
    long_data_tidyr <- pivot_longer(large_data, cols = starts_with("var"), 
                                    names_to = "variable", values_to = "value")
  },
  data_table_melt = {
    long_data_dt <- melt(large_data, id.vars = "id", variable.name = "variable", 
                         value.name = "value")
  },
  times = 10
)

print(m14)
## Unit: microseconds
##                expr      min       lq      mean   median       uq       max
##  tidyr_pivot_longer 6143.391 6216.117 7734.5541 6279.240 6361.909 20979.150
##     data_table_melt  464.346  497.208  566.4518  529.207  593.286   760.268
##  neval
##     10
##     10
fig <- fbox_plot(m14, "microseconds")
fig